package org.huatek.anders.utils;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.stream.Stream;

/**
 * @author anders
 * @date 2018-12-16 15:49
 **/
public class CommonUtils {
    private final static String[] RANDOM_STR_CODE = {"z","x","c","v","b","n","m","l","k","j","h","g","f","d","s","a","q","w","e","r","t","y","u","i","o","p","Q","W","E","R","T","Y","U","I","O","P","A","S","D","F","G","H","J","K","L","Z","X","C","V","B","N","M","1","2","3","4","5","6","7","8","9","0"};
    /**
     * 随机数
     * @param min 从...开始
     * @param max 从...结束
     * @param n 生成个数
     * @return
     */
    public static Integer[] randomNumber(Integer min, Integer max, Integer n){
        if (n > (max - min + 1) || max < min) {
            return null;
        }
        Set<Integer> set = new HashSet<>(n);
        while(set.size()<n) {
            Stream.generate(Math::random).limit(n).forEach(i -> set.add((int) (i * (max - min)+min)));
        }
        return  set.toArray(new Integer[n]);
    }

    /**
     * 随机字符串
     * @param len 个数
     * @return 字符串
     */
    public static String randomChar(int len) {
        StringBuffer sb = new StringBuffer(len);
        Stream.iterate(0,i->i+1).limit(len).forEach(s->{
            sb.append(RANDOM_STR_CODE[new Random().nextInt(RANDOM_STR_CODE.length)]);
        });
        return sb.toString();
    }
    public static void main(String[] args){
        System.out.println(randomChar(6));
        Arrays.stream(randomNumber(1, 30, 5)).forEach(System.out::println);


    }
}
